#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "board.h"
#include "paint.h"
#include "mouse.h"
#include "computer.h"

int piece[ROW][COL];
int step;

static int grid_size;
static int piece_radius;
static int backgrand[2][2];
static int grid[ROW][COL][2];

static int old_r = -1;
static int old_c = -1;

int is_in(int r, int c)
{
	return r >= 0 && r < ROW && c >= 0 && c < COL;
}

int is_empty(int r, int c)
{
	return is_in(r, c) && piece[r][c] == 0;
}

static int is_mouse_on_piece(int *r, int *c)
{
	int i, j;

	for (i = 0; i < ROW; ++i)
		for (j = 0; j < COL; ++j)
			if (fabs(mouse_x - grid[i][j][0]) < grid_size / 2 && fabs(mouse_y - grid[i][j][1]) < grid_size / 2)
			{
				*r = i;
				*c = j;
				return 1;
			}

	return 0;
}

static void frame(int x1, int y1, int x2, int y2)
{
	line(x1, y1, x2, y1);
	line(x2, y1, x2, y2);
	line(x2, y2, x1, y2);
	line(x1, y2, x1, y1);
}

static void init_board()
{
	int i, j;

	grid_size = GRIDSIZE;
	piece_radius = GRIDSIZE * 4 / 10;

	step = 0;
	grid[0][0][0] = fb_width / 2 - ROW / 2 * grid_size;
	grid[0][0][1] = fb_height / 2 - COL / 2 * grid_size;
	for (i = 0; i < ROW; ++i)
		for (j = 0; j < COL; ++j)
		{
			piece[i][j] = 0;
			grid[i][j][0] = grid[0][0][0] + j * grid_size;
			grid[i][j][1] = grid[0][0][1] + i * grid_size;
		}

	backgrand[0][0] = grid[0][0][0] - grid_size;
	backgrand[0][1] = grid[0][0][1] - grid_size;
	backgrand[1][0] = grid[ROW - 1][COL - 1][0] + grid_size;
	backgrand[1][1] = grid[ROW - 1][COL - 1][1] + grid_size;
}

void show_board()
{
	int i, j;
	int r = 1;
	int d = 3;

	system("clear");

	init_board();

	set_color(GRAY);
	rectangle(backgrand[0][0], backgrand[0][1], backgrand[1][0], backgrand[1][1]);

	set_color(BLACK);
	frame(grid[0][0][0] - d, grid[0][0][1] - d, grid[ROW - 1][COL - 1][0] + d, grid[ROW - 1][COL - 1][1] + d);

	for (i = 0; i < ROW; ++i)
		line(grid[i][0][0], grid[i][0][1], grid[i][COL - 1][0], grid[i][COL - 1][1]);
	for (j = 0; j < COL; ++j)
		line(grid[0][j][0], grid[0][j][1], grid[ROW - 1][j][0], grid[ROW - 1][j][1]);

	circle(grid[ROW / 2][COL / 2][0], grid[ROW / 2][COL / 2][1], r);
	circle(grid[3][3][0], grid[3][3][1], r);
	circle(grid[3][11][0], grid[3][11][1], r);
	circle(grid[11][3][0], grid[11][3][1], r);
	circle(grid[11][11][0], grid[11][11][1], r);
}

void show_piece(int r, int c, int w)
{
	if (!is_in(r, c))
		return;

	hide_mouse();

	w == 1 ? set_color(BLACK) : set_color(WHITE);
	circle(grid[r][c][0], grid[r][c][1], piece_radius);

	w == 1 ? set_color(WHITE) : set_color(BLACK);
	pixel(grid[r][c][0], grid[r][c][1]);

	w == 1 ? set_color(WHITE) : set_color(BLACK);
	if (old_r != -1)
		circle(grid[old_r][old_c][0], grid[old_r][old_c][1], piece_radius);

	old_r = r;
	old_c = c;

	show_mouse();

	piece[r][c] = w;
	step += 1;
}

int is_win(int current_row, int current_col)
{
	int i, j, r, c, w;
	int dx[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };
	int dy[8] = { -1, -1, -1, 0, 0, 1, 1, 1 };
	int dn[8] = { 1, 1, 1, 1, 1, 1, 1, 1 };

	if (is_empty(current_row, current_col) || !is_in(current_row, current_col))
		return 0;

	w = piece[current_row][current_col];
	for (i = 0; i < 8; ++i)
	{
		r = current_row;
		c = current_col;
		for (j = 1; j < NUM; ++j)
		{
			r += dy[i];
			c += dx[i];
			if (is_in(r, c) && w == piece[r][c])
				++dn[i];
			else
				break;
		}
	}
	if (dn[0] + dn[7] > NUM || dn[1] + dn[6] > NUM || dn[2] + dn[5] > NUM || dn[3] + dn[4] > NUM)
		return 1;

	return 0;
}


int mouse_input(int *r, int *c, int w)
{
	return mouse_clicked() && is_mouse_on_piece(r, c) && is_empty(*r, *c);
}

int computer_input(int *r, int *c, int w)
{
	analysis(r, c, w);

	return is_empty(*r, *c);
}
